<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>排名列表</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../styles/common.css" />
    <link rel="stylesheet" href="../styles/rankings.css" />
    <style>
        #ranking-app .notAdmin::after {
            border: 0 !important;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="ranking-app">
    <div class="container">
        <div class="search-bar">
            <el-input
                    v-model="nation"
                    placeholder="输入地区"
                    style="width: 200px; margin-right: 20px;"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="handleQuery"
                ></i>
            </el-input>
            <el-input
                    v-model="field"
                    placeholder="输入领域"
                    style="width: 200px; margin-right: 20px;"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="handleQuery"
                ></i>
            </el-input>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
        </div>
        <el-table
                :data="filteredTableData"
                stripe
                class="tableBox"
        >
            <el-table-column
                    prop="userRank"
                    label="排名"
            ></el-table-column>
            <el-table-column
                    prop="name"
                    label="名字"
            ></el-table-column>
            <el-table-column
                    prop="score"
                    label="分数"
            ></el-table-column>
            <el-table-column
                    prop="nation"
                    label="地区"
            ></el-table-column>
            <el-table-column
                    prop="field"
                    label="领域"
            ></el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[5, 10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
</div>

<script src="../plugins/vue/vue.js"></script>
<script src="../plugins/element-ui/index.js"></script>
<script src="../plugins/axios/axios.min.js"></script>
<script src="../js/new_request.js"></script>
<script src="../js/validate.js"></script>
<script src="../js/index.js"></script>
<script src="../api/ranking.js"></script>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script>
    new Vue({
        el: '#ranking-app',
        data() {
            return {
                nation: '',
                field: '',
                counts: 0,
                page: 1,
                pageSize: 5,
                tableData: [
                    { userRank: 1, name: '小明', score: 100, nation: '中国', field: '人工智能' },
                    { userRank: 2, name: '小红', score: 95, nation: '美国', field: '数据科学' },
                    { userRank: 3, name: '小刚', score: 90, nation: '日本', field: '机器学习' }
                    // 可以添加更多数据
                ],
            }
        },
        computed: {
            filteredTableData() {
                return this.tableData.map(item => {
                    // 处理score: 截取到3位小数
                    // item.score = item.score ? item.score.toFixed(3) : 'N/A';
                    item.score = item.score.toFixed(3);
                    // 处理field: 去掉可能的中括号
                    item.field = item.field && item.field.trim() !== '[]'? item.field.replace(/\[|\]/g, '').trim() : 'N/A';

                    // 处理nation: 如果为空或者空字符串，显示 N/A
                    item.nation = item.nation && item.nation.trim() !== '' ? item.nation : 'N/A';

                    return item;
                });
            }
        },
        created() {
            this.pageSize = 5;
            this.init();
            this.field = "";
            this.nation = "";
        },
        methods: {
            async init() {
                // 这里可以调用后端接口来获取数据
                console.log(this.page)
                console.log(this.pageSize)
                const response = await getRankingData({ page: this.page, pageSize: this.pageSize, nation: this.nation, field: this.field });
                this.tableData = response.data.records;
                this.counts = response.data.total;
            },
            handleQuery() {
                this.page = 1; // 重置为第一页
                this.init();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.init();
            },
            handleCurrentChange(val) {
                this.page = val;
                this.init();
            }
        }
    });
</script>
</body>
</html>
